package com.fsm.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/*******************
 *@Describe
 *@author 封神梦
 *@Time 2023/6/13
 *@Company http://com.yiview.com
 ****************/
public class HashRedisService {

    @Autowired
    private RedisTemplate redisTemplate;

    public void set(String key, String value) {
        //1、通过redisTemplate设置值
        redisTemplate.boundHashOps("HashKey").put("SmallKey", "HashVaue");

//2、通过BoundValueOperations设置值
        BoundHashOperations hashKey = redisTemplate.boundHashOps("HashKey");
        hashKey.put("SmallKey", "HashVaue");

//3、通过ValueOperations设置值
        HashOperations hashOps = redisTemplate.opsForHash();
        hashOps.put("HashKey", "SmallKey", "HashVaue");

    }

    public void addMap() {
        HashMap<String, String> hashMap = new HashMap<>();
        redisTemplate.boundHashOps("HashKey").putAll(hashMap);

    }

    public void setExpire() {
        redisTemplate.boundValueOps("HashKey").expire(1, TimeUnit.MINUTES);
        redisTemplate.expire("HashKey", 1, TimeUnit.MINUTES);

    }

    public void getAllKey() {
        //1、通过redisTemplate获取值
        Set keys1 = redisTemplate.boundHashOps("HashKey").keys();

//2、通过BoundValueOperations获取值
        BoundHashOperations hashKey = redisTemplate.boundHashOps("HashKey");
        Set keys2 = hashKey.keys();

//3、通过ValueOperations获取值
        HashOperations hashOps = redisTemplate.opsForHash();
        Set keys3 = hashOps.keys("HashKey");

    }

    public void getAllValue() {
        //1、通过redisTemplate获取值
        List values1 = redisTemplate.boundHashOps("HashKey").values();

//2、通过BoundValueOperations获取值
        BoundHashOperations hashKey = redisTemplate.boundHashOps("HashKey");
        List values2 = hashKey.values();

//3、通过ValueOperations获取值
        HashOperations hashOps = redisTemplate.opsForHash();
        List values3 = hashOps.values("HashKey");

    }

    public void getValueByKey(String key) {
        //1、通过redisTemplate获取
        String value1 = (String) redisTemplate.boundHashOps("HashKey").get("SmallKey");

//2、通过BoundValueOperations获取值
        BoundHashOperations hashKey = redisTemplate.boundHashOps("HashKey");
        String value2 = (String) hashKey.get("SmallKey");

//3、通过ValueOperations获取值
        HashOperations hashOps = redisTemplate.opsForHash();
        String value3 = (String) hashOps.get("HashKey", "SmallKey");

    }

    public void getMap() {
        //1、通过redisTemplate获取
        Map entries = redisTemplate.boundHashOps("HashKey").entries();

//2、通过BoundValueOperations获取值
        BoundHashOperations hashKey = redisTemplate.boundHashOps("HashKey");
        Map entries1 = hashKey.entries();

//3、通过ValueOperations获取值
        HashOperations hashOps = redisTemplate.opsForHash();
        Map entries2 = hashOps.entries("HashKey");

    }

    //获取值
    public String getValue() {
        //1、通过redisTemplate设置值
        String str1 = (String) redisTemplate.boundValueOps("StringKey").get();

        //2、通过BoundValueOperations获取值
        BoundValueOperations stringKey = redisTemplate.boundValueOps("StringKey");
        String str2 = (String) stringKey.get();

        //3、通过ValueOperations获取值
        ValueOperations ops = redisTemplate.opsForValue();
        String str3 = (String) ops.get("StringKey");
        return str3;
    }

    //4)、删除key
    public boolean deleteKey() {
        //删除小key
        redisTemplate.boundHashOps("HashKey").delete("SmallKey");
        //删除大key
        return redisTemplate.delete("HashKey");

    }

    public Boolean checkExit() {
        return  redisTemplate.boundHashOps("HashKey").hasKey("SmallKey");

    }
}
